package com.dsteiner.gwt.cafeteriasophia.client;



import com.dsteiner.gwt.cafeteriasophia.client.controller.ProductController;
import com.dsteiner.gwt.cafeteriasophia.client.controller.PurchaseController;
import com.dsteiner.gwt.cafeteriasophia.client.controller.SaleLineController;
import com.dsteiner.gwt.cafeteriasophia.client.model.ProductModel;
import com.dsteiner.gwt.cafeteriasophia.client.model.PurchaseModel;
import com.dsteiner.gwt.cafeteriasophia.client.model.SaleLineModel;
import com.dsteiner.widgets.client.PrefilledTextBox;
import com.dsteiner.widgets.client.Widgets.FlowPanelExt;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.HorizontalPanelExt;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.IWidgetBuilder;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ProductWidget extends LazyWidget implements IWidgetBuilder{

	public class PurchaseWidget extends LazyWidget {

		private PurchaseController purchaseController = new PurchaseController();
		private final PurchaseModel purchaseModel;

		public PurchaseWidget(PurchaseModel purchaseModel) {
			this.purchaseModel = purchaseModel;
			setStyleName("purchase-widget");
		}

		@Override
		public Widget createContentWidget() {
			PrefilledTextBoxModel priceTextBoxModel = new PrefilledTextBoxModel();
			priceTextBoxModel.setDefaultText("ingrese el precio");
			PrefilledTextBoxModel amountTextBoxModel = new PrefilledTextBoxModel();
			amountTextBoxModel.setDefaultText("ingrese la compra (por ejemplo 5.0)");
			HorizontalPanelExt lineWidget = createLineWidget("Compra", amountTextBoxModel);
			PrefilledTextBox priceTextBox = new PrefilledTextBox(priceTextBoxModel);
			
			HorizontalPanelExt pricePanel = new HorizontalPanelExt();
			pricePanel.add(new HTML("precio"));
			pricePanel.add((Widget)priceTextBox);
			lineWidget.add((Widget)pricePanel);
			
			priceTextBox.addStyleName("price-text-box");
			
			purchaseController.registerPurchasesTextBoxModel(amountTextBoxModel);
			purchaseController.registerPurchasePriceTextBoxModel(priceTextBoxModel);
			purchaseController.registerPurchasePricePanel(pricePanel);
			purchaseController.registerPurchaseWidget(this);
			purchaseController.registerPurchaseModel(purchaseModel);
			
			return lineWidget;
		}

	}
	private class SaleLineWidget extends LazyWidget{

		private SaleLineController saleLineController = new SaleLineController();
		private SaleLineModel saleLineModel;

		public SaleLineWidget(SaleLineModel saleLineModel) {
			this.saleLineModel = saleLineModel;
		}

		@Override
		public Widget createContentWidget() {
			PrefilledTextBoxModel salePriceTextBoxModel = new PrefilledTextBoxModel();
			salePriceTextBoxModel.setDefaultText("ingrese el precio");
			PrefilledTextBoxModel salesTextBoxModel = new PrefilledTextBoxModel();
			salesTextBoxModel.setDefaultText("ingrese la venta (por ejemplo 10.0)");
			HorizontalPanelExt salesLine = createLineWidget("Venta", salesTextBoxModel);
			PrefilledTextBox salePriceTextBox = new PrefilledTextBox(salePriceTextBoxModel);
			
			HorizontalPanelExt salesPricePanel = new HorizontalPanelExt();
			salesPricePanel.add(new HTML("precio"));
			salesPricePanel.add((Widget)salePriceTextBox);
			salesLine.add((Widget)salesPricePanel);
			
			saleLineController.registerSalesPricePanel(salesPricePanel);
			saleLineController.registerSaleLineModel(saleLineModel);
			saleLineController.registerSaleLineWidget(this);
			saleLineController.registerSalesTextBoxModel(salesTextBoxModel);
			saleLineController.registerPriceTextBoxModel(salePriceTextBoxModel);
			salePriceTextBox.addStyleName("price-text-box");
			
			return salesLine;
		}
		
	}
	
	private ProductController productController = new ProductController();
	private final ProductModel productModel;

	public ProductWidget(ProductModel productModel) {
		this.productModel = productModel;
		setStyleName("product-widget");
	}

	@Override
	public Widget createContentWidget() {
		FlowPanel mainPanel = new FlowPanel();
		HTMLExt titleHtml = new HTMLExt();
		PrefilledTextBoxModel purchasesTextBoxModel = new PrefilledTextBoxModel();
		PrefilledTextBoxModel stockTextBoxModel = new PrefilledTextBoxModel();
		PrefilledTextBoxModel incomeTextBoxModel = new PrefilledTextBoxModel();
		purchasesTextBoxModel.setDefaultText("ingrese la compra (por ejemplo 5.0)");
		stockTextBoxModel.setDefaultText("ingrese el inventario (por ejemplo 20.0)");
		incomeTextBoxModel.setDefaultText("ingrese algun ingreso aparte de ventas (por ejemplo 20.0)");
		HorizontalPanelExt stockLine = createLineWidget("Inventario", stockTextBoxModel);
		HorizontalPanelExt incomeLine = createLineWidget("Ingreso", incomeTextBoxModel);
		FlowPanelExt salePanel = new FlowPanelExt();
		FlowPanelExt salesWidgetContentPanel = new FlowPanelExt();
		FlowPanelExt purchasesWidgetContentPanel = new FlowPanelExt();
		HorizontalPanel salesActionPanel = new HorizontalPanel();
		HTMLExt addSaleWidgetLink = new HTMLExt("<a href=\"javascript:void(0);\">aggregar mas ventas</a>");
		HTMLExt reduceSaleWidgetLink = new HTMLExt("<a href=\"javascript:void(0);\">reducir ventas</a>");
		
		salesActionPanel.setSpacing(5);
		salesActionPanel.add(addSaleWidgetLink);
		salesActionPanel.add(reduceSaleWidgetLink);
		
		salePanel.add((Widget)salesWidgetContentPanel);
		salePanel.add(salesActionPanel);
		
		mainPanel.add(titleHtml);
		mainPanel.add((Widget)stockLine);
		mainPanel.add((Widget)salePanel);
		mainPanel.add((Widget)incomeLine);
		mainPanel.add((Widget)purchasesWidgetContentPanel);
		
		titleHtml.addStyleName("title-html");
		
		productController.registerWidgetBuilder(this);
		productController.registerAddSaleWidgetLink(addSaleWidgetLink);
		productController.registerReduceSaleWidgetLink(reduceSaleWidgetLink);
		productController.registerSalesWidgetContentPanel(salesWidgetContentPanel);
		productController.registerPurchasesWidgetContentPanel(purchasesWidgetContentPanel);
		productController.registerStockTextBoxModel(stockTextBoxModel);
		productController.registerIncomeTextBoxModel(incomeTextBoxModel);
		productController.registerStockLine(stockLine);
		productController.registerIncomeLine(incomeLine);
		productController.registerSalePanel(salePanel);
		productController.registerTitleHtml(titleHtml);
		productController.registerProductWidget(this);
		productController.registerProductModel(productModel);
		return mainPanel;
	}

	public HorizontalPanelExt createLineWidget(String text, PrefilledTextBoxModel prefilledTextBoxModel) {
		HorizontalPanelExt horizontalPanel = new HorizontalPanelExt();
		HTML html = new HTML(text);
		PrefilledTextBox prefilledTextBox = new PrefilledTextBox(prefilledTextBoxModel);
		
		horizontalPanel.add(html);
		horizontalPanel.add((Widget)prefilledTextBox);
//		horizontalPanel.setCellHorizontalAlignment(prefilledTextBox, HasHorizontalAlignment.ALIGN_RIGHT);

		prefilledTextBox.setWidth("300px");
		html.setWidth("100px");
//		horizontalPanel.setWidth("100%");
		horizontalPanel.addStyleName("line-widget");
		return horizontalPanel;
	}

	public IWidget createWidget(Object model) {
		IWidget widget = null;
		if(model instanceof SaleLineModel){
			widget = new SaleLineWidget((SaleLineModel) model);
		}
		if(model instanceof PurchaseModel){
			widget = new PurchaseWidget((PurchaseModel) model);
		}
		return widget;
	}
	
}